package com.uhylab.uhuvinetest.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.google.gson.Gson;
import com.uhylab.uhuvinetest.R;
import com.uhylab.uhuvinetest.adapter.UHNewsListAdapter;
import com.uhylab.uhuvinetest.base.UHBase;
import com.uhylab.uhuvinetest.base.UHBaseActivity;
import com.uhylab.uhuvinetest.bean.NewsInformation;
import com.uhylab.uhuvinetest.utils.refresh.MyRefreshLayout;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONObject;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;


/**
 * 项目名称：Uhylab
 * 类描述：资讯页面
 * 创建人：xyb
 * 创建时间：16/6/8 14:48
 * 修改人：xyb
 * 修改时间：16/6/8 14:48
 * 修改备注：
 */
public class UHNewsActivity extends UHBaseActivity implements AdapterView.OnItemClickListener, MyRefreshLayout.OnLoadListener, MyRefreshLayout.OnRefreshListener {

    @BindView(R.id.webView_back)
    ImageView mWebViewBack;
    @BindView(R.id.webview)
    ListView mWebview;
    @BindView(R.id.myRefresh)
    MyRefreshLayout mMyRefresh;
    private int load = 0;

    private UHNewsListAdapter mUHNewsListAdapter;

    @Override
    public int bindLayout() {
        return R.layout.activity_news;
    }

    @Override
    public void initView(View view) {
        ButterKnife.bind(this);
        mUHNewsListAdapter = new UHNewsListAdapter(this);
        mWebview.setAdapter(mUHNewsListAdapter);
        mWebview.setOnItemClickListener(this);
    }

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    public void doBusiness(Context mContext) {
        mMyRefresh.setOnLoadListener(this);
        mMyRefresh.setOnRefreshListener(this);
        initNewsList(false, "0", false);
    }

    private void initNewsList(final boolean isRefresh, String offset, final boolean isLoading) {
        OkHttpUtils
                .get()
                .url(UHBase.information)
                .addParams("genre", "1")
                .addParams("appstyle", "2")
                .addParams("offset", offset)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(String response, int id) {
                        try {
                            Gson gson = new Gson();
                            JSONObject jsonObject = new JSONObject(response);
                            String code = (String) jsonObject.get("code");
                            if (isRefresh) {
                                mMyRefresh.setRefreshing(false);
                            } else if (isLoading) {
                                mMyRefresh.setLoading(false);
                            }
                            if (code.equals("0000")) {
                                NewsInformation newsInformation = gson.fromJson(response, NewsInformation.class);
                                List<NewsInformation.DataBean> data = newsInformation.getData();
                                if (isRefresh) {
                                    mUHNewsListAdapter.clear();
                                    mUHNewsListAdapter.addListDate(data);

                                } else if (isLoading) {
                                    mUHNewsListAdapter.addListDate(data);
                                } else {
                                    mUHNewsListAdapter.addListDate(data);
                                }
                            }

                        } catch (Exception e) {
                            e.printStackTrace();

                        }
                    }
                });
    }

    @Override
    public void resume() {

    }

    @Override
    public void destroy() {

    }


    @OnClick({R.id.webView_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.webView_back:
                finish();
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        NewsInformation.DataBean dataBean = (NewsInformation.DataBean) parent.getItemAtPosition(position);
        String id1 = dataBean.getId();
        String loadUrl = dataBean.getUrl();
        startActivity(new Intent(this, UHNewsWebActivity.class).putExtra("id", id1).putExtra("title", dataBean.getTitle())
                .putExtra("url", loadUrl));

    }

    @Override
    public void onLoad() {
        load += 10;
        initNewsList(false, Integer.toString(load), true);
    }

    @Override
    public void onRefresh() {
        load = 0;
        initNewsList(true, "0", false);
    }


}
